<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="七牛云公钥" prop="qiNiuAccessKey">
      <el-input v-model="dataForm.qiNiuAccessKey" placeholder="七牛云公钥"></el-input>
    </el-form-item>
    <el-form-item label="七牛云私钥" prop="qiNiuSecretKey">
      <el-input v-model="dataForm.qiNiuSecretKey" placeholder="七牛云私钥"></el-input>
    </el-form-item>
    <el-form-item label="邮箱账号" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱账号"></el-input>
    </el-form-item>
    <el-form-item label="邮箱发件人用户名" prop="emailUserName">
      <el-input v-model="dataForm.emailUserName" placeholder="邮箱发件人用户名"></el-input>
    </el-form-item>
    <el-form-item label="邮箱密码" prop="emailPassword">
      <el-input v-model="dataForm.emailPassword" placeholder="邮箱密码"></el-input>
    </el-form-item>
    <el-form-item label="SMTP地址" prop="smtpAddress">
      <el-input v-model="dataForm.smtpAddress" placeholder="SMTP地址"></el-input>
    </el-form-item>
    <el-form-item label="SMTP端口" prop="smtpPort">
      <el-input v-model="dataForm.smtpPort" placeholder="SMTP端口"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="七牛云上传空间" prop="qiNiuBucket">
      <el-input v-model="dataForm.qiNiuBucket" placeholder="七牛云上传空间"></el-input>
    </el-form-item>
    <el-form-item label="七牛云存储区域 华东（z0），华北(z1)，华南(z2)，北美(na0)，东南亚(as0)" prop="qiNiuArea">
      <el-input v-model="dataForm.qiNiuArea" placeholder="七牛云存储区域 华东（z0），华北(z1)，华南(z2)，北美(na0)，东南亚(as0)"></el-input>
    </el-form-item>
    <el-form-item label="图片是否上传七牛云 (0:否， 1：是)" prop="uploadQiNiu">
      <el-input v-model="dataForm.uploadQiNiu" placeholder="图片是否上传七牛云 (0:否， 1：是)"></el-input>
    </el-form-item>
    <el-form-item label="图片是否上传本地存储 (0:否， 1：是)" prop="uploadLocal">
      <el-input v-model="dataForm.uploadLocal" placeholder="图片是否上传本地存储 (0:否， 1：是)"></el-input>
    </el-form-item>
    <el-form-item label="图片显示优先级（ 1 展示 七牛云,  0 本地）" prop="picturePriority">
      <el-input v-model="dataForm.picturePriority" placeholder="图片显示优先级（ 1 展示 七牛云,  0 本地）"></el-input>
    </el-form-item>
    <el-form-item label="七牛云域名前缀：http://images.moguit.cn" prop="qiNiuPictureBaseUrl">
      <el-input v-model="dataForm.qiNiuPictureBaseUrl" placeholder="七牛云域名前缀：http://images.moguit.cn"></el-input>
    </el-form-item>
    <el-form-item label="本地服务器域名前缀：http://localhost:8600" prop="localPictureBaseUrl">
      <el-input v-model="dataForm.localPictureBaseUrl" placeholder="本地服务器域名前缀：http://localhost:8600"></el-input>
    </el-form-item>
    <el-form-item label="是否开启邮件通知(0:否， 1:是)" prop="startEmailNotification">
      <el-input v-model="dataForm.startEmailNotification" placeholder="是否开启邮件通知(0:否， 1:是)"></el-input>
    </el-form-item>
    <el-form-item label="编辑器模式，(0：富文本编辑器CKEditor，1：markdown编辑器Veditor)" prop="editorModel">
      <el-input v-model="dataForm.editorModel" placeholder="编辑器模式，(0：富文本编辑器CKEditor，1：markdown编辑器Veditor)"></el-input>
    </el-form-item>
    <el-form-item label="主题颜色" prop="themeColor">
      <el-input v-model="dataForm.themeColor" placeholder="主题颜色"></el-input>
    </el-form-item>
    <el-form-item label="Minio远程连接地址" prop="minioEndPoint">
      <el-input v-model="dataForm.minioEndPoint" placeholder="Minio远程连接地址"></el-input>
    </el-form-item>
    <el-form-item label="Minio公钥" prop="minioAccessKey">
      <el-input v-model="dataForm.minioAccessKey" placeholder="Minio公钥"></el-input>
    </el-form-item>
    <el-form-item label="Minio私钥" prop="minioSecretKey">
      <el-input v-model="dataForm.minioSecretKey" placeholder="Minio私钥"></el-input>
    </el-form-item>
    <el-form-item label="Minio桶" prop="minioBucket">
      <el-input v-model="dataForm.minioBucket" placeholder="Minio桶"></el-input>
    </el-form-item>
    <el-form-item label="图片是否上传Minio (0:否， 1：是)" prop="uploadMinio">
      <el-input v-model="dataForm.uploadMinio" placeholder="图片是否上传Minio (0:否， 1：是)"></el-input>
    </el-form-item>
    <el-form-item label="Minio服务器文件域名前缀" prop="minioPictureBaseUrl">
      <el-input v-model="dataForm.minioPictureBaseUrl" placeholder="Minio服务器文件域名前缀"></el-input>
    </el-form-item>
    <el-form-item label="是否开启仪表盘通知(0:否， 1:是)" prop="openDashboardNotification">
      <el-input v-model="dataForm.openDashboardNotification" placeholder="是否开启仪表盘通知(0:否， 1:是)"></el-input>
    </el-form-item>
    <el-form-item label="仪表盘通知【用于首次登录弹框】" prop="dashboardNotification">
      <el-input v-model="dataForm.dashboardNotification" placeholder="仪表盘通知【用于首次登录弹框】"></el-input>
    </el-form-item>
    <el-form-item label="博客详情图片显示优先级（ 0:本地  1: 七牛云 2: Minio）" prop="contenuPicturePriority">
      <el-input v-model="dataForm.contenuPicturePriority" placeholder="博客详情图片显示优先级（ 0:本地  1: 七牛云 2: Minio）"></el-input>
    </el-form-item>
    <el-form-item label="是否开启用户邮件激活功能【0 关闭，1 开启】" prop="openEmailActivate">
      <el-input v-model="dataForm.openEmailActivate" placeholder="是否开启用户邮件激活功能【0 关闭，1 开启】"></el-input>
    </el-form-item>
    <el-form-item label="搜索模式【0:SQL搜索 、1：全文检索】" prop="searchModel">
      <el-input v-model="dataForm.searchModel" placeholder="搜索模式【0:SQL搜索 、1：全文检索】"></el-input>
    </el-form-item>
    <el-form-item label="阿里云对象存储桶名" prop="aliyunBucketName">
      <el-input v-model="dataForm.aliyunBucketName" placeholder="阿里云对象存储桶名"></el-input>
    </el-form-item>
    <el-form-item label="阿里云对象存储Endpoint" prop="aliyunEndpoint">
      <el-input v-model="dataForm.aliyunEndpoint" placeholder="阿里云对象存储Endpoint"></el-input>
    </el-form-item>
    <el-form-item label="阿里云对象存储公钥" prop="aliyunAccessKey">
      <el-input v-model="dataForm.aliyunAccessKey" placeholder="阿里云对象存储公钥"></el-input>
    </el-form-item>
    <el-form-item label="阿里云对象存储私钥" prop="aliyunAccessKeySecret">
      <el-input v-model="dataForm.aliyunAccessKeySecret" placeholder="阿里云对象存储私钥"></el-input>
    </el-form-item>
    <el-form-item label="是否上传阿里云对象存储" prop="uploadAlioss">
      <el-input v-model="dataForm.uploadAlioss" placeholder="是否上传阿里云对象存储"></el-input>
    </el-form-item>
    <el-form-item label="阿里云对象存储访问域名" prop="aliossPictureBaseUrl">
      <el-input v-model="dataForm.aliossPictureBaseUrl" placeholder="阿里云对象存储访问域名"></el-input>
    </el-form-item>
    <el-form-item label="阿里云对象存储上传目录" prop="aliyunCatalog">
      <el-input v-model="dataForm.aliyunCatalog" placeholder="阿里云对象存储上传目录"></el-input>
    </el-form-item>
    <el-form-item label="是否删除:0否 1是" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="是否删除:0否 1是"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          qiNiuAccessKey: '',
          qiNiuSecretKey: '',
          email: '',
          emailUserName: '',
          emailPassword: '',
          smtpAddress: '',
          smtpPort: '',
          status: '',
          createTime: '',
          updateTime: '',
          qiNiuBucket: '',
          qiNiuArea: '',
          uploadQiNiu: '',
          uploadLocal: '',
          picturePriority: '',
          qiNiuPictureBaseUrl: '',
          localPictureBaseUrl: '',
          startEmailNotification: '',
          editorModel: '',
          themeColor: '',
          minioEndPoint: '',
          minioAccessKey: '',
          minioSecretKey: '',
          minioBucket: '',
          uploadMinio: '',
          minioPictureBaseUrl: '',
          openDashboardNotification: '',
          dashboardNotification: '',
          contenuPicturePriority: '',
          openEmailActivate: '',
          searchModel: '',
          aliyunBucketName: '',
          aliyunEndpoint: '',
          aliyunAccessKey: '',
          aliyunAccessKeySecret: '',
          uploadAlioss: '',
          aliossPictureBaseUrl: '',
          aliyunCatalog: '',
          isDelete: ''
        },
        dataRule: {
          qiNiuAccessKey: [
            { required: true, message: '七牛云公钥不能为空', trigger: 'blur' }
          ],
          qiNiuSecretKey: [
            { required: true, message: '七牛云私钥不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱账号不能为空', trigger: 'blur' }
          ],
          emailUserName: [
            { required: true, message: '邮箱发件人用户名不能为空', trigger: 'blur' }
          ],
          emailPassword: [
            { required: true, message: '邮箱密码不能为空', trigger: 'blur' }
          ],
          smtpAddress: [
            { required: true, message: 'SMTP地址不能为空', trigger: 'blur' }
          ],
          smtpPort: [
            { required: true, message: 'SMTP端口不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          qiNiuBucket: [
            { required: true, message: '七牛云上传空间不能为空', trigger: 'blur' }
          ],
          qiNiuArea: [
            { required: true, message: '七牛云存储区域 华东（z0），华北(z1)，华南(z2)，北美(na0)，东南亚(as0)不能为空', trigger: 'blur' }
          ],
          uploadQiNiu: [
            { required: true, message: '图片是否上传七牛云 (0:否， 1：是)不能为空', trigger: 'blur' }
          ],
          uploadLocal: [
            { required: true, message: '图片是否上传本地存储 (0:否， 1：是)不能为空', trigger: 'blur' }
          ],
          picturePriority: [
            { required: true, message: '图片显示优先级（ 1 展示 七牛云,  0 本地）不能为空', trigger: 'blur' }
          ],
          qiNiuPictureBaseUrl: [
            { required: true, message: '七牛云域名前缀：http://images.moguit.cn不能为空', trigger: 'blur' }
          ],
          localPictureBaseUrl: [
            { required: true, message: '本地服务器域名前缀：http://localhost:8600不能为空', trigger: 'blur' }
          ],
          startEmailNotification: [
            { required: true, message: '是否开启邮件通知(0:否， 1:是)不能为空', trigger: 'blur' }
          ],
          editorModel: [
            { required: true, message: '编辑器模式，(0：富文本编辑器CKEditor，1：markdown编辑器Veditor)不能为空', trigger: 'blur' }
          ],
          themeColor: [
            { required: true, message: '主题颜色不能为空', trigger: 'blur' }
          ],
          minioEndPoint: [
            { required: true, message: 'Minio远程连接地址不能为空', trigger: 'blur' }
          ],
          minioAccessKey: [
            { required: true, message: 'Minio公钥不能为空', trigger: 'blur' }
          ],
          minioSecretKey: [
            { required: true, message: 'Minio私钥不能为空', trigger: 'blur' }
          ],
          minioBucket: [
            { required: true, message: 'Minio桶不能为空', trigger: 'blur' }
          ],
          uploadMinio: [
            { required: true, message: '图片是否上传Minio (0:否， 1：是)不能为空', trigger: 'blur' }
          ],
          minioPictureBaseUrl: [
            { required: true, message: 'Minio服务器文件域名前缀不能为空', trigger: 'blur' }
          ],
          openDashboardNotification: [
            { required: true, message: '是否开启仪表盘通知(0:否， 1:是)不能为空', trigger: 'blur' }
          ],
          dashboardNotification: [
            { required: true, message: '仪表盘通知【用于首次登录弹框】不能为空', trigger: 'blur' }
          ],
          contenuPicturePriority: [
            { required: true, message: '博客详情图片显示优先级（ 0:本地  1: 七牛云 2: Minio）不能为空', trigger: 'blur' }
          ],
          openEmailActivate: [
            { required: true, message: '是否开启用户邮件激活功能【0 关闭，1 开启】不能为空', trigger: 'blur' }
          ],
          searchModel: [
            { required: true, message: '搜索模式【0:SQL搜索 、1：全文检索】不能为空', trigger: 'blur' }
          ],
          aliyunBucketName: [
            { required: true, message: '阿里云对象存储桶名不能为空', trigger: 'blur' }
          ],
          aliyunEndpoint: [
            { required: true, message: '阿里云对象存储Endpoint不能为空', trigger: 'blur' }
          ],
          aliyunAccessKey: [
            { required: true, message: '阿里云对象存储公钥不能为空', trigger: 'blur' }
          ],
          aliyunAccessKeySecret: [
            { required: true, message: '阿里云对象存储私钥不能为空', trigger: 'blur' }
          ],
          uploadAlioss: [
            { required: true, message: '是否上传阿里云对象存储不能为空', trigger: 'blur' }
          ],
          aliossPictureBaseUrl: [
            { required: true, message: '阿里云对象存储访问域名不能为空', trigger: 'blur' }
          ],
          aliyunCatalog: [
            { required: true, message: '阿里云对象存储上传目录不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除:0否 1是不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/user/systemconfig/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.qiNiuAccessKey = data.systemConfig.qiNiuAccessKey
                this.dataForm.qiNiuSecretKey = data.systemConfig.qiNiuSecretKey
                this.dataForm.email = data.systemConfig.email
                this.dataForm.emailUserName = data.systemConfig.emailUserName
                this.dataForm.emailPassword = data.systemConfig.emailPassword
                this.dataForm.smtpAddress = data.systemConfig.smtpAddress
                this.dataForm.smtpPort = data.systemConfig.smtpPort
                this.dataForm.status = data.systemConfig.status
                this.dataForm.createTime = data.systemConfig.createTime
                this.dataForm.updateTime = data.systemConfig.updateTime
                this.dataForm.qiNiuBucket = data.systemConfig.qiNiuBucket
                this.dataForm.qiNiuArea = data.systemConfig.qiNiuArea
                this.dataForm.uploadQiNiu = data.systemConfig.uploadQiNiu
                this.dataForm.uploadLocal = data.systemConfig.uploadLocal
                this.dataForm.picturePriority = data.systemConfig.picturePriority
                this.dataForm.qiNiuPictureBaseUrl = data.systemConfig.qiNiuPictureBaseUrl
                this.dataForm.localPictureBaseUrl = data.systemConfig.localPictureBaseUrl
                this.dataForm.startEmailNotification = data.systemConfig.startEmailNotification
                this.dataForm.editorModel = data.systemConfig.editorModel
                this.dataForm.themeColor = data.systemConfig.themeColor
                this.dataForm.minioEndPoint = data.systemConfig.minioEndPoint
                this.dataForm.minioAccessKey = data.systemConfig.minioAccessKey
                this.dataForm.minioSecretKey = data.systemConfig.minioSecretKey
                this.dataForm.minioBucket = data.systemConfig.minioBucket
                this.dataForm.uploadMinio = data.systemConfig.uploadMinio
                this.dataForm.minioPictureBaseUrl = data.systemConfig.minioPictureBaseUrl
                this.dataForm.openDashboardNotification = data.systemConfig.openDashboardNotification
                this.dataForm.dashboardNotification = data.systemConfig.dashboardNotification
                this.dataForm.contenuPicturePriority = data.systemConfig.contenuPicturePriority
                this.dataForm.openEmailActivate = data.systemConfig.openEmailActivate
                this.dataForm.searchModel = data.systemConfig.searchModel
                this.dataForm.aliyunBucketName = data.systemConfig.aliyunBucketName
                this.dataForm.aliyunEndpoint = data.systemConfig.aliyunEndpoint
                this.dataForm.aliyunAccessKey = data.systemConfig.aliyunAccessKey
                this.dataForm.aliyunAccessKeySecret = data.systemConfig.aliyunAccessKeySecret
                this.dataForm.uploadAlioss = data.systemConfig.uploadAlioss
                this.dataForm.aliossPictureBaseUrl = data.systemConfig.aliossPictureBaseUrl
                this.dataForm.aliyunCatalog = data.systemConfig.aliyunCatalog
                this.dataForm.isDelete = data.systemConfig.isDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/systemconfig/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'qiNiuAccessKey': this.dataForm.qiNiuAccessKey,
                'qiNiuSecretKey': this.dataForm.qiNiuSecretKey,
                'email': this.dataForm.email,
                'emailUserName': this.dataForm.emailUserName,
                'emailPassword': this.dataForm.emailPassword,
                'smtpAddress': this.dataForm.smtpAddress,
                'smtpPort': this.dataForm.smtpPort,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'qiNiuBucket': this.dataForm.qiNiuBucket,
                'qiNiuArea': this.dataForm.qiNiuArea,
                'uploadQiNiu': this.dataForm.uploadQiNiu,
                'uploadLocal': this.dataForm.uploadLocal,
                'picturePriority': this.dataForm.picturePriority,
                'qiNiuPictureBaseUrl': this.dataForm.qiNiuPictureBaseUrl,
                'localPictureBaseUrl': this.dataForm.localPictureBaseUrl,
                'startEmailNotification': this.dataForm.startEmailNotification,
                'editorModel': this.dataForm.editorModel,
                'themeColor': this.dataForm.themeColor,
                'minioEndPoint': this.dataForm.minioEndPoint,
                'minioAccessKey': this.dataForm.minioAccessKey,
                'minioSecretKey': this.dataForm.minioSecretKey,
                'minioBucket': this.dataForm.minioBucket,
                'uploadMinio': this.dataForm.uploadMinio,
                'minioPictureBaseUrl': this.dataForm.minioPictureBaseUrl,
                'openDashboardNotification': this.dataForm.openDashboardNotification,
                'dashboardNotification': this.dataForm.dashboardNotification,
                'contenuPicturePriority': this.dataForm.contenuPicturePriority,
                'openEmailActivate': this.dataForm.openEmailActivate,
                'searchModel': this.dataForm.searchModel,
                'aliyunBucketName': this.dataForm.aliyunBucketName,
                'aliyunEndpoint': this.dataForm.aliyunEndpoint,
                'aliyunAccessKey': this.dataForm.aliyunAccessKey,
                'aliyunAccessKeySecret': this.dataForm.aliyunAccessKeySecret,
                'uploadAlioss': this.dataForm.uploadAlioss,
                'aliossPictureBaseUrl': this.dataForm.aliossPictureBaseUrl,
                'aliyunCatalog': this.dataForm.aliyunCatalog,
                'isDelete': this.dataForm.isDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
